package cn.dt70.service.impl;

import cn.dt70.dao.aqbReward.AqbRewardMapper;
import cn.dt70.pojo.AqbReward;
import cn.dt70.service.AqbRewardService;
import cn.dt70.utils.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("AqbRewardService")
public class AqbRewardServiceImpl implements AqbRewardService {

    private static final Logger LOGGER = Logger.getLogger(AqbRewardServiceImpl.class);

    @Resource
    private AqbRewardMapper aqbRewardMapper;

    @Override
    public boolean getReward(AqbReward aqbReward) throws Exception {
        int row=0;
        try {
            row=aqbRewardMapper.getReward(aqbReward);
            if(row==1){
                LOGGER.debug("aqbRewardMapper.getReward 新增成功");
                return true;
            }else{
                LOGGER.debug("aqbRewardMapper.getReward 新增失败");
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("aqbRewardMapper.getReward 新增异常"+e.getMessage());
            return false;
        }
    }

    @Override
    public boolean updateStatus(Integer rewordId) throws Exception {
        int row=0;
        try {
            row=aqbRewardMapper.udpRewardStatus(rewordId);
            if(row==1){
                LOGGER.debug("aqbRewardMapper.udpRewardStatus 修改成功");
                return true;
            }else{
                LOGGER.debug("aqbRewardMapper.udpRewardStatus 修改失败");
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("aqbRewardMapper.udpRewardStatus 修改异常"+e.getMessage());
            return false;
        }
    }

    @Override
    public Integer sumReward(Integer userId, Integer rewardType) throws Exception {

        int sum = 0;
        try {
            sum = aqbRewardMapper.couldUseReward(userId,rewardType);
            System.out.println("可用奖励金的总额为"+sum);
        } catch (Exception e) {
            LOGGER.error("aqbRewardMapper.rewardSum 计算红包总数异常"+e.getMessage());
        }
        return sum;
    }

    @Override
    public List<AqbReward> getAllRewardByStatus(Integer userId, Integer rewardType, Integer startDay) throws Exception {
        List<AqbReward> rewards = new ArrayList<AqbReward>();
        try {
            rewards = aqbRewardMapper.queryByStatus(userId,rewardType,startDay);
            if (null !=rewards ||("").equals(rewards)){
                LOGGER.debug("aqbRewardMapper.rewardList得到所有的奖励金成功");
            }else {
                LOGGER.debug("aqbRewardMapper.rewardList得到所有的奖励金失败");
            }

        } catch (Exception e) {
            LOGGER.error("aqbRewardMapper.getAllReward得到所有的奖励金异常"+e.getMessage());
        }
        return rewards;
    }

    @Override
    public List<AqbReward> getAllRewardByStatusAndTime(Integer userId, Integer rewardType, Date begin, Date end) throws Exception {
        List<AqbReward> rewards = new ArrayList<AqbReward>();
        try {
            rewards = aqbRewardMapper.queryByStatusTime(userId,rewardType,begin,end);
            if (null !=rewards ||("").equals(rewards)){
                LOGGER.debug("aqbRewardMapper.rewardList得到所有的奖励金成功");
            }else {
                LOGGER.debug("aqbRewardMapper.rewardList得到所有的奖励金失败");
            }

        } catch (Exception e) {
            LOGGER.error("aqbRewardMapper.getAllReward得到所有的奖励金异常"+e.getMessage());
        }
        return rewards;
    }

    @Override
    public List<AqbReward> getAllReward(Integer userId,
                                        Integer rewardType,
                                        Integer startDay,
                                        PageSupport ps) throws Exception {
        List<AqbReward> rewards = new ArrayList<AqbReward>();
        try {
            int count = aqbRewardMapper.rewardCount(userId,rewardType,startDay);
            ps.setTotalCount(count);
            int s = (ps.getCurrentPageNo()-1)*ps.getPageSize();
            int a = ps.getPageSize();
            rewards = aqbRewardMapper.rewardList(userId,rewardType,startDay,s,a);
            if (null !=rewards ||("").equals(rewards)){
                LOGGER.debug("aqbRewardMapper.rewardList得到所有的奖励金成功");
            }else {
                LOGGER.debug("aqbRewardMapper.rewardList得到所有的奖励金失败");
            }

        } catch (Exception e) {
            LOGGER.error("aqbRewardMapper.getAllReward得到所有的奖励金异常"+e.getMessage());
        }
        return rewards;
    }

    @Override
    public List<AqbReward> getAllRewardByTime(Integer userId, Integer rewardType, Date begin, Date end, PageSupport ps) throws Exception {
        List<AqbReward> rewards = new ArrayList<AqbReward>();
        try {
            int count = aqbRewardMapper.rewardCountTime(userId,rewardType,begin,end);
            ps.setTotalCount(count);
            int s = (ps.getCurrentPageNo()-1)*ps.getPageSize();
            int a = ps.getPageSize();
            rewards = aqbRewardMapper.rewardListBetweenTime(userId,rewardType,s,a,begin,end);
            if (null !=rewards ||("").equals(rewards)){
                LOGGER.debug("aqbRewardMapper.rewardListBetweenTime得到所有的奖励金成功");
            }else {
                LOGGER.debug("aqbRewardMapper.rewardListBetweenTime得到所有的奖励金失败");
            }

        } catch (Exception e) {
            LOGGER.error("aqbRewardMapper.rewardListBetweenTime得到所有的奖励金异常"+e.getMessage());
        }
        return rewards;
    }

    @Override
    public List<AqbReward> getRewards(Integer userId) throws Exception {
        List<AqbReward> rewards = new ArrayList<AqbReward>();

        try {
            rewards=aqbRewardMapper.rewardLists(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rewards;
    }

    @Override
    public int surplus(Integer userId,Integer rewardType) throws Exception {
        int  surplusSum = aqbRewardMapper.couldUseReward(userId,rewardType)-aqbRewardMapper.haveUsedReward(userId);
        return surplusSum;
    }

    @Override
    public AqbReward rewardInformation(Integer userId) throws Exception {

            AqbReward aqbReward = aqbRewardMapper.rewardInformation(userId);
            return aqbReward;
    }
}
